<template>
  <div class="login-container">
    <!-- 登录 -->
    <div class="login-wrap">
      <div class="login">
        <div class="loginform">
          <ul class="tab clearFix">
            <li>
              <a style="border-right: 0">扫描登录</a>
            </li>
            <li>
              <a class="current">账户登录</a>
            </li>
          </ul>

          <div class="content">
            <el-form :model="ruleForm" ref="formRef" :rules="rules" label-width="80px" :inline="false" size="normal"
              status-icon>
              <el-form-item label="手机号 :" prop="phone">
                <el-input v-model="ruleForm.phone" placeholder="请输入你的手机号"></el-input>
              </el-form-item>
              <el-form-item label="密码 :" prop="password">
                <el-input v-model="ruleForm.password" placeholder="请输入密码" type="password"></el-input>
              </el-form-item>
            </el-form>
            <div class="setting clearFix">
              <label class="checkbox inline">
                <input name="m1" type="checkbox" value="2" checked />
                自动登录
              </label>
              <span class="forget">忘记密码？</span>
            </div>
            <!-- 阻止默认事件 -->
            <button class="btn" @click.prevent="handleLogin">
              登&nbsp;&nbsp;录
            </button>
            <div class="call clearFix">
              <ul>
                <li><img src="./images/qq.png" alt="" /></li>
                <li><img src="./images/sina.png" alt="" /></li>
                <li><img src="./images/ali.png" alt="" /></li>
                <li><img src="./images/weixin.png" alt="" /></li>
              </ul>
              <router-link class="register" to="/register">立即注册</router-link>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>
<script setup lang='ts'>
import { useRouter,useRoute } from 'vue-router'
import { reactive, ref } from 'vue';
import { ElMessage, type FormInstance, type FormRules } from 'element-plus'
import userBaseInfo from '@/store/modules/login'
const userBaseInfoStore = userBaseInfo()
const router = useRouter()
const route = useRoute()
const redirect = route.query.redirect

const formRef = ref<FormInstance>()
const ruleForm = reactive({
  phone: '13111111111',
  password: '111111'
})

// 手机密码验证
const validateUsername = (rule: any, value: any, callback: any) => {
  if (value.length < 4) {
    callback(new Error('用户名长度不能小于4位'))
  } else {
    callback()
  }
}
const validatePassword = (rule: any, value: any, callback: any) => {
  if (value.length < 6) {
    callback(new Error('密码长度不能小于6位'))
  } else {
    callback()
  }
}
const rules = {
  username: [{ required: true, validator: validateUsername }],
  password: [{ required: true, trigger: 'blur', validator: validatePassword }]
}

//登录
const handleLogin = async () => {
  // const loginInfo = JSON.parse(localStorage.getItem('userInfo'))
  await formRef.value?.validate()
  // lodaing.value = true
  const { password, phone } = ruleForm
  try {
    await userBaseInfoStore.login({ phone, password })
    if(redirect){
      router.push(redirect as string)
    }else{
    router.push(`/`)
    }
  } catch (error) {
    ElMessage({
      type: "error",
      message: '登录失败'
    })
  }
}
</script>

<style  lang="less" scoped>
.login-wrap {
  height: 487px;
  background-color: #f5f7f9;
  background: url(./images/login.jpg) no-repeat;
  background-size: 100% 487px;
}

.login {
  width: 1200px;
  height: 487px;
  margin: 0 auto;

}

/* 登录的表单 */
.loginform {
  width: 420px;
  height: 406px;
  box-sizing: border-box;
  background: rgba(255, 255, 255, .6);
  float: left;
  padding: 20px;
  margin-top: 30px;
}

/* 扫描登录和账号登录 */
.tab li {
  width: 50%;
  float: left;
  text-align: center;
}

.tab li a {
  width: 100%;
  display: block;
  height: 50px;
  line-height: 50px;
  font-size: 20px;
  font-weight: 700;
  color: #333;
  border: 1px solid #ddd;
  box-sizing: border-box;
  text-decoration: none;
}

/* 账户登录 */
.current {
  border-bottom: none;
  border-top-color: #28a3ef;
  color: #e1251b;
}

/* 账户和密码的样式 */
.content {
  width: 380px;
  height: 316px;
  box-sizing: border-box;
  border: 1px solid #ddd;
  border-top: none;
  margin: 20px 0 10px 0;
  padding: 18px;
}

.content form {
  margin: 40px 0 15px 0;
  font-size: 12px;
  line-height: 18px;
}

.setting label {
  float: left;
}

.setting .forget {
  float: right;
}

.btn {
  background-color: #e1251b;
  padding: 6px;
  border-radius: 0;
  font-size: 16px;
  font-family: 微软雅黑;
  word-spacing: 4px;
  border: 1px solid #e1251b;
  color: #fff;
  width: 100%;
  height: 36px;
  margin-top: 25px;
  outline: none;
}

.call {
  margin-top: 30px;
}

.call ul {
  float: left;
}

.call ul li {
  float: left;
  margin-right: 5px;
}

.register {
  float: right;
  font-size: 15px;
  line-height: 38px;
}

.register:hover {
  color: #4cb9fc;
  text-decoration: underline;
}
</style>